<?php

require_once ("DbConfig.php");

/**
 * tohle sem nasel v komentech na cz.php.net - v podstate je
 * to predelavka na parametrizovane dotazy.. dost drsne
 * 
 * func_num_args() - zjisti pocet argumentu volane funkce
 * func_get_args() - vrati pole techto argumentu (z te funkce)
 * call_func_array("jmeno_funkce", $pole_argumentu) - zavola uzivatelem (systemem) definovanou funkci
 * a preda ji parametr/y z $pole_argumentu
 * 
 * 
 * function mysql_queryf($query) {
    if (func_num_args()>1) {
        $args=func_get_args();
        $query=call_user_func_array("sprintf",$args);
    }
    return mysql_query($query);
 }

	// Allows for things like
	$resultset=mysql_queryf("SELECT * FROM `table` WHERE `id`=%u LIMIT 1", $_GET["id"]);
	// $_GET["id"] will be converted to an integer: 0 if it's invalid
 
 **/
class AbstractDb {
	
	/**
	 * Objekt pripojeni
	 */
	private $connection;
	
	private $affRows;
	
	
	//konstruktor
	public function __construct(){
		
	}

	/**
	 * Mozna by se to dalo jeste predelat, tohle je jen muj prvni napad
	 * Napada me jeste dalsi reseni, to kdyztak vytunim/e pak :]
	 */
	private function connect(){
		
		$url = DbConfig::getInstance()->getDbUrl();
		$username = DbConfig::getInstance()->getDbUsername();
		$password = DbConfig::getInstance()->getDbPassword(); 
		$dbname = DbConfig::getInstance()->getDbName();
		
		$this->connection = mysql_connect($url,$username,$password);
		//mysql_set_charset("utf-8",$this->connection);
		mysql_select_db($dbname,$this->connection);
		
	}
	
	private function closeConnection(){
		mysql_close($this->connection);
	}

	/**
	 * Vraci result dane query, cili zpracovani je na nadtride..
	 */
	public function query($query){
	
		$this->connect();
		$result = mysql_query($query,$this->connection);
		$this->affRows = mysql_affected_rows();
		$this->closeConnection();
		
		return $result;
	}
	
	public function getAffectedRows(){
		return $this->affRows;
	}
	
}

?>
